unit SmallStruct.View.TSSView;

interface

uses
  Classes,
  SmallStruct.Core,
  SmallStruct.Core.TSSObjectContext,
  SmallStruct.View,
  SmallStruct.Form.TSSFrame,
  SmallStruct.Model,
  cxContainer,
  cxEdit,
  Controls,
  ExtCtrls,
  RzPanel;

type
  TSSViewContext = class(TSSObjectContext, ISSViewContext)
  private
    _Resource: ISSObjectContainer;
    _ModelContainer: ISSObjectContainer;
  public
    function GetResource: ISSObjectContainer;
    function GetModelContainer: ISSObjectContainer;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TSSView = class(TSSFrame, ISSView)
    _EditStyleController: TcxEditStyleController;
    _FramePanel: TRzPanel;
  private
    _ModelContainer: ISSObjectContainer;
  public
    function GetModelContainer: ISSObjectContainer;
  protected
    function QueryModel(const AModelName: string): ISSModel;
    function GetPrimaryModel: ISSModel;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TSSViewClass = class of TSSView;

implementation

uses SmallStruct.Core.TSSObjectContainer,
  SmallStruct.Error,
  SmallStruct.Configuration;

{$R *.dfm}

{ TSSView }

constructor TSSView.Create(AOwner: TComponent);
begin
  Name := '';
  inherited;
  _ModelContainer := TSSObjectContainer.Create(Self);
  { TFrame Setting }
  Align := alClient;
end;

destructor TSSView.Destroy;
begin
  _ModelContainer.ReleaseObjectInstance;
  inherited;
end;

function TSSView.GetModelContainer: ISSObjectContainer;
begin
  Result := _ModelContainer;
end;

function TSSView.QueryModel(const AModelName: string): ISSModel;
begin
  if _ModelContainer.FindObject(AModelName) >= 0 then
  begin
    Result := _ModelContainer.GetObject(AModelName) as ISSModel;
    Exit;
  end
  else
  begin
    if (GetContext as ISSViewContext).GetParameter.FindObject(AModelName) >= 0 then
    begin
      Result := (GetContext as ISSViewContext).GetParameter.GetObject(AModelName) as ISSModel;
      Exit;
    end
    else
      raise ESSObjectNotFound.Create(ClassName, 'QueryModel', AModelName)
  end;
end;

function TSSView.GetPrimaryModel: ISSModel;
begin
  Result := QueryModel((GetContext.GetConfiguration as ISSViewConfiguration).GetPrimaryModelName);
end;

{ TSSViewContext }

constructor TSSViewContext.Create(AOwner: TComponent);
begin
  inherited;
  _Resource := TSSObjectContainer.Create(Self);
  _ModelContainer := TSSObjectContainer.Create(Self);
end;

function TSSViewContext.GetModelContainer: ISSObjectContainer;
begin
  Result := _ModelContainer;
end;

function TSSViewContext.GetResource: ISSObjectContainer;
begin
  Result := _Resource;
end;

destructor TSSViewContext.Destroy;
begin
  _Resource.ReleaseObjectInstance;
  inherited;
end;

end.
